import {
  constantRouterMap
} from '@/router'
import {
  listMyModule
} from '@/api/login'
import Layout from '@/views/layout/Layout'

const permission = {
  state: {
    routers: [],
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({
      commit
    }) {
      const sysModules = {
        path: '/sys',
        component: Layout,
        name: '系统管理',
        children: [],
        meta: {
          title: '系统管理',
          icon: 'edit'
        },
        alwaysShow: true
      }
      const basModules = {
        path: '/bas',
        component: Layout,
        name: '基础管理',
        children: [],
        meta: {
          title: '基础管理',
          icon: 'edit'
        },
        alwaysShow: true
      }
      const vchModules = {
        path: '/vch',
        component: Layout,
        name: '业务管理',
        children: [],
        meta: {
          title: '业务管理',
          icon: 'edit'
        },
        alwaysShow: true
      }
      const rptModules = {
        path: '/rpt',
        component: Layout,
        name: '报表管理',
        children: [],
        meta: {
          title: '报表管理',
          icon: 'edit'
        },
        alwaysShow: true
      }
      return new Promise((resolve, reject) => {
        listMyModule().then(response => {
          const data = response.Data
          const routers = []
          for (const idx in data) {
            if (data[idx].Path) {
              const item = {
                path: data[idx].Path,
                name: data[idx].Name,

                component: () => import('@/views/app/' + data[idx].Path),
                alwaysShow: true,
                meta: {
                  title: data[idx].Name,
                  icon: 'edit',
                  create: data[idx].Create,
                  modify: data[idx].Modify,
                  delete: data[idx].Delete,
                  approve: data[idx].Approve,
                  unapprove: data[idx].UnApprove,
                  func1: data[idx].Func1,
                  func2: data[idx].Func2,
                  func3: data[idx].Func3,
                  func4: data[idx].Func4
                }
              }
              switch (data[idx].Class) {
                case 1:
                  sysModules.children.push(item)
                  break
                case 2:
                  basModules.children.push(item)
                  break
                case 3:
                  vchModules.children.push(item)
                  break
                case 4:
                  rptModules.children.push(item)
                  break
              }
            }
          }
          if (sysModules.children.length > 0) {
            routers.push(sysModules)
          }
          if (basModules.children.length > 0) {
            routers.push(basModules)
          }
          if (vchModules.children.length > 0) {
            routers.push(vchModules)
          }
          if (rptModules.children.length > 0) {
            routers.push(rptModules)
          }
          routers.push({
            path: '*',
            redirect: '/404',
            hidden: true
          })
          commit('SET_ROUTERS', routers)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default permission
